//
//  UIOptionView.m
//  AccuroFit
//
//  Created by NCIT Developer on 1/12/16.
//  Copyright © 2016 Nci Technology Inc. All rights reserved.
//
#import <AudioToolbox/AudioServices.h>

#import "AppDelegate.h"
#import "HrdRcdViewCtrl.h"

#import "UIOptionView.h"

#define _nActTypeCount 5

@implementation UIOptionView {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
  UILabel* _lblMsgInfo;
  short _nShowType;

  UIButton* _btnActWalk;
  UIButton* _btnActRunning;
  UIButton* _btnActBiking;
  UIButton* _btnActClibing;
  UIButton* _btnActGroup;
  
  id _optDelegate;
  NSInteger _nHrateId;
  short _nOptTypeIdx;
  
  UIButton* _btnCancel;
  UIButton* _btnSyncUpld;
  
  UIImageView* _ivRoundPoint[_nActTypeCount];
}


- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    //UIColor* colBkgd = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"ic-bottbkgd-t.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    //self.backgroundColor = colBkgd;
    
    //_nSelBtn = -1;
    [self initialize: frame];
  }
  self.backgroundColor = [UIColor colorWithWhite:0.25 alpha:0.98];
  //self.backgroundColor = [UIColor colorWithRed:0.123 green:0.123 blue:0.123 alpha:0.720];
  
  _nHrateId = 0;
  _nOptTypeIdx = -1;
  _nShowType = 0;
  
  return self;
}

- (void) initialize:(CGRect)frame
{
  AppDelegate *pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  CGFloat W = frame.size.width; //ip5/5s: 256, iP6: 300, iP6p :331
  
  CGFloat yyPos = 20;
  _lblMsgInfo = [[UILabel alloc] initWithFrame:CGRectMake(20, yyPos, W-40, 25)];
  _lblMsgInfo.textColor = [UIColor whiteColor];
  _lblMsgInfo.font = [UIFont systemFontOfSize:16.0];
  _lblMsgInfo.backgroundColor = [UIColor clearColor];
  _lblMsgInfo.textAlignment = NSTextAlignmentCenter;
  [self addSubview: _lblMsgInfo];
  
  //BOOL bIpad = W > 700;
  CGRect rt = CGRectMake(0, 0, W, frame.size.height);
  CGFloat xMargin = 6.0;//W / 20.0;
  CGFloat yMargin = 60.0;//frame.size.height / 20.0;
  CGFloat fSize = W / 5.0 - xMargin * 2;
  CGFloat fxOffset = W / 5.0;
  
  rt.origin.x = xMargin;
  rt.origin.y = yMargin;
  rt.size.width = fSize;
  rt.size.height = fSize;
  
  CGFloat fImgSize = 40;
  
  UIImage* iCon = [pApp imageWithImage:[UIImage imageNamed:@"act-walking.png"] scaledToSize:CGSizeMake(fImgSize, fImgSize)];
  _btnActWalk = [[UIButton alloc] initWithFrame:rt];
  [_btnActWalk setImage:iCon forState:UIControlStateNormal];
  [_btnActWalk addTarget:self action:@selector(tapOptionButs:) forControlEvents:UIControlEventTouchUpInside];
  //[self addSubview:_btnWiFiType];
   //[_btnWiFiType[1] setBackgroundImage:[UIImage imageNamed:@"ic-uarrow_sel.png"] forState:UIControlStateSelected];
  
  rt.origin.x += fxOffset;
  iCon = [pApp imageWithImage:[UIImage imageNamed:@"act-running.png"] scaledToSize:CGSizeMake(fImgSize, fImgSize)];
  _btnActRunning = [[UIButton alloc] initWithFrame:rt];
  [_btnActRunning setImage:iCon forState:UIControlStateNormal];
  [_btnActRunning addTarget:self action:@selector(tapOptionButs:) forControlEvents:UIControlEventTouchUpInside];
  //[self addSubview:_btnBleType];
  
  rt.origin.x += fxOffset;
  iCon = [pApp imageWithImage:[UIImage imageNamed:@"act-biking.png"] scaledToSize:CGSizeMake(fImgSize, fImgSize)];
  //rt.origin.y += W / 2.0;
  _btnActBiking = [[UIButton alloc] initWithFrame:rt];
  [_btnActBiking setImage:iCon forState:UIControlStateNormal];
  [_btnActBiking addTarget:self action:@selector(tapOptionButs:) forControlEvents:UIControlEventTouchUpInside];
  //[self addSubview:_btnBcaType];
  
  rt.origin.x += fxOffset;
  iCon = [pApp imageWithImage:[UIImage imageNamed:@"act-climbing.png"] scaledToSize:CGSizeMake(fImgSize, fImgSize)];
  _btnActClibing = [[UIButton alloc] initWithFrame:rt];
  [_btnActClibing setImage:iCon forState:UIControlStateNormal];
  [_btnActClibing addTarget:self action:@selector(tapOptionButs:) forControlEvents:UIControlEventTouchUpInside];
  // [self addSubview:_btnManual];
  
  rt.origin.x += fxOffset;
  iCon = [pApp imageWithImage:[UIImage imageNamed:@"act-group.png"] scaledToSize:CGSizeMake(fImgSize, fImgSize)];
  _btnActGroup = [[UIButton alloc] initWithFrame:rt];
  [_btnActGroup setImage:iCon forState:UIControlStateNormal];
  [_btnActGroup addTarget:self action:@selector(tapOptionButs:) forControlEvents:UIControlEventTouchUpInside];
  
  CGFloat xPos = 30;
  CGFloat btnWid = 100;
  rt.origin.x = W / 2 - xPos - btnWid;
  rt.origin.y =  frame.size.height - btnWid * 0.45;
  rt.size.width = btnWid;
  rt.size.height = btnWid / 4;
  _btnCancel = [[UIButton alloc] initWithFrame:rt];
  _btnCancel.layer.masksToBounds = YES;
  _btnCancel.layer.cornerRadius = 4;
  [_btnCancel setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
  _btnCancel.backgroundColor = [UIColor lightGrayColor];
  [_btnCancel addTarget:self action:@selector(tapBottBtns:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:_btnCancel];
  
  
  rt.origin.x = W / 2 + xPos;
  _btnSyncUpld = [[UIButton alloc] initWithFrame:rt];
  _btnSyncUpld.layer.masksToBounds = YES;
  _btnSyncUpld.layer.cornerRadius = 4;
  [_btnSyncUpld setTitle:@"Upload" forState:UIControlStateNormal];
  _btnSyncUpld.backgroundColor = [UIColor lightGrayColor];
  [_btnSyncUpld addTarget:self action:@selector(tapBottBtns:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:_btnSyncUpld];
  
  //selected round point
  rt.origin.x = xMargin + rt.size.width / 4 - 4;
  rt.origin.y = yMargin + rt.size.height + 30;
  if(W > 614) { //iPad
    rt.origin.x += 24;
    rt.origin.y += 24;
  }
  else if(W < 331 && W >= 300) {
    rt.origin.x -= 5;
    rt.origin.y -= 8;
  }
  else if(W <= 256) {
    rt.origin.x -= 7.5;
    rt.origin.y -= 15;
  }
  
  rt.size.width = 16;
  rt.size.height = 16;
  
  iCon = [pApp imageWithImage:[UIImage imageNamed:@"ic-rdSelPt.png"] scaledToSize:CGSizeMake(12, 12)];
  for(int i=0; i<_nActTypeCount; i++) {
    _ivRoundPoint[i] = [[UIImageView alloc] initWithImage:iCon];
    _ivRoundPoint[i].frame = rt;
    [self addSubview:_ivRoundPoint[i]];
    _ivRoundPoint[i].hidden = YES;
    rt.origin.x += fxOffset;
  }
  
}

- (void) drawRect:(CGRect)rect
{
  /*CGContextRef ctx = UIGraphicsGetCurrentContext();
  
  CGFloat rd = 15.0;
  CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
  CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
  
  if(_cptQRSession == nil)
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.1, 0.1, 1.0);
  else
    CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 0.95);
  
  CGContextSetLineWidth(ctx, 2.75);
  //drae rect
  CGContextMoveToPoint(ctx, minx, midy);
  // Add an arc through 2 to 3
  CGContextAddArcToPoint(ctx, minx, miny, midx, miny, rd);
  // Add an arc through 4 to 5
  CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, rd);
  // Add an arc through 6 to 7
  CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, rd);
  // Add an arc through 8 to 9
  CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, rd);
  // Close the path
  CGContextClosePath(ctx);
  // Fill & stroke the path
  CGContextDrawPath(ctx, kCGPathStroke);*/
}

- (void) tapBottBtns:(id)sender
{
  if(sender == _btnCancel) {
    _nOptTypeIdx = 254;
  }
  if(_nOptTypeIdx == -1 || _nOptTypeIdx == 0 || _nOptTypeIdx == 255) {
    _lblMsgInfo.text = NSLocalizedString(@"please select one", nil);
    AudioServicesPlaySystemSound(1112);
    return;
  }
  [self rmvOptTypeButs];
  if (_optDelegate && [_optDelegate respondsToSelector:@selector(onOptTypeChgedOk:type:show:)]) {
    [_optDelegate onOptTypeChgedOk:_nHrateId type:_nOptTypeIdx show:_nShowType];
  }
}

- (void) tapOptionButs:(id)sender
{
  if(sender == _btnActWalk) {
    _nOptTypeIdx = 1;
    //AudioServicesPlaySystemSound(1115);
  }
  else if(sender == _btnActRunning) {
    _nOptTypeIdx = 2;
    //AudioServicesPlaySystemSound(1315);
  }
  else if(sender == _btnActBiking) {
    _nOptTypeIdx = 3;
    //AudioServicesPlaySystemSound(1100);
  }
  else if(sender == _btnActClibing) {
    _nOptTypeIdx = 4;
  }
  else {
    _nOptTypeIdx = 5;
    //AudioServicesPlaySystemSound(1109);
  }
  [self setBottImgShowHide:_nOptTypeIdx];
}

- (void) setBottImgShowHide:(short)nOptIdx
{
  int nIdx = nOptIdx - 1;
  for(int i=0; i<_nActTypeCount; i++) {
    BOOL bHide = nIdx != i;
    _ivRoundPoint[i].hidden = bHide;
  }
}

- (void) addOptTypeButs:(NSInteger)nHrateId type:(short)nOptIdx info:(NSString*)sMsg show:(short)nShow
{
  _nHrateId =nHrateId;
  _nOptTypeIdx = nOptIdx;
  _lblMsgInfo.text = sMsg;
  [self setBottImgShowHide:nOptIdx];
  _nShowType = nShow;
  
  [self addSubview:_btnActWalk];
  [self addSubview:_btnActRunning];
  [self addSubview:_btnActBiking];
  [self addSubview:_btnActClibing];
  [self addSubview:_btnActGroup];
  
  if(nShow == 1) {
    [_btnSyncUpld setTitle:@"Update" forState:UIControlStateNormal];
  }
}

- (void) rmvOptTypeButs
{
  NSArray *vToRemove = [self subviews];
  for (UIView *v in vToRemove) {
    [v removeFromSuperview];
  }
  
  //if(_cptQRSession != nil) {
    //[self stopQRReading];
  //}
  
  self.backgroundColor = [UIColor colorWithWhite:0.128 alpha:0.98];
  //_bQrReading = NO;
}

@end
